import java.io.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Tasks {
    
   private String record;
   private String status="Unmarked";
   private String fileName = "Submission.txt";
    
       
    public boolean submitAssignment(Assignment assignmentObj) 
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        Date date = new Date();
        assignmentObj.setAssignmentDate(dateFormat.format(date).toString()) ;

        
        try {
            
            BufferedWriter buffwriter = new BufferedWriter(new FileWriter(fileName,true));
                    
            record = assignmentObj.getStudentObj().getStudentId()+" "+ assignmentObj.getSubjectObj().getName()+" " + assignmentObj.getName()+" "+ status + " " + assignmentObj.getSubmitDate()+" " + assignmentObj.getAssingmentID();

		//+" " +  assignmentObj.getAssingmentID() +" " + assignmentObj.getStudentObj().getFirstName() +" "+ assignmentObj.getStudentObj().getLastName() ;
            buffwriter.write(record);
            buffwriter.newLine();
            buffwriter.close();
       } catch (Exception e) {
            System.out.println("problem while writing the file" + e);
            return false;
        }

        return true;
    }

    public void UploadSubmission(String Path,String StudId,String SubName,String AssName)
    {
        try
        {
            
            int index1 = Path.lastIndexOf(".");
            int index2 = Path.length();
           
            String index3 = Path.substring(index1,index2);
            File inputFile = new File(Path);
            
            File outputFile = new File("c:\\java proj\\Assignments\\"+ StudId +"_"+ SubName +"_"+AssName+index3);

             FileReader in = new FileReader(inputFile);
             FileWriter out = new FileWriter(outputFile);
                int c;

                while ((c = in.read()) != -1)
                out.write(c);

                in.close();
                out.close();
        }catch (IOException ex)
        {

        }

    }
    public boolean viewAssignmentDetails(String searchStr)
    {
        try {
          		StringBuffer contents = new StringBuffer();
			    BufferedReader reader = null;
        		String fileContent = null;
			    File file = new File(fileName);
			    reader = new BufferedReader(new FileReader(file));
		 
		    try
			{  
			//Reading the file content, repeat until all lines is read
            	while ((fileContent = reader.readLine()) != null)
            	{
            		//extract the content to compare to the input
                	String [] contentStr= fileContent.split("\\s"); 
                	String theText=contentStr[0].toString()+" "
                		+contentStr[1].toString()+" "
                			+contentStr[2].toString();
                	
                	//whether the searching is matched
         			if(theText.equalsIgnoreCase(searchStr))
         			{
         				System.out.println("StudentID	Subject	Assignment  	Status        Submit Date/Time    Assignment ID");
         				System.out.println("---------------------------------------------------------------------------------------");
         				System.out.print(contentStr[0].toString()+"		"
         									+contentStr[1].toString()+"		"
         										+contentStr[2].toString()+"		"
         											+contentStr[3].toString()+"      " //output details
                                                        + contentStr[4].toString() +"-" + contentStr[5].toString()+ "     "
                                                             + contentStr[6].toString());
         				//if(contentStr.length==5)
         				//	System.out.print(" "+contentStr[4].toString());
         				return true;
         				//System.exit(1);
                        
         			}
	            	}
	            		System.out.println("Could not find any detail matched to your search.");
                        return false;
                    } //end try
                    catch (FileNotFoundException e)
                    {
                        System.out.println("File is not found.");
                        return false;
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                        return false;
                    }
 		        }catch(Exception e)
                 {
            		 System.out.println( "record not found");
                     return false;
                 }
              
    	}
       
}
